﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compilex.Automata;
using Compilex.BaseLibrary;

namespace Compilex.Lexer.Automata
{
    public class FloatDFA : DFA
    {
        public FloatDFA()
        {
            this.StartState = BuildAutomaton();
        }

        private DFAState BuildAutomaton()
        {
            DFAState startState = new DFAState("q0");
            DFAState atLeastOneDigit = new DFAState("q1");
            DFAState pointState = new DFAState("q2");
            DFAState exponentState = new DFAState("q3");
            DFAState acceptState = new DFAState("q4", true);

            startState.AddTransition(StringUtils.IsDigit, atLeastOneDigit);

            atLeastOneDigit.AddTransition(StringUtils.IsDigit, atLeastOneDigit);
            atLeastOneDigit.AddTransition('.', pointState);

            pointState.AddTransition(StringUtils.IsDigit, acceptState);

            acceptState.AddTransition(c => c == "e" || c == "E", exponentState);
            acceptState.AddTransition(StringUtils.IsDigit, acceptState);

            exponentState.AddTransition(c => c == "+" || c == "-", exponentState);
            exponentState.AddTransition(StringUtils.IsDigit, acceptState);

            return startState;
        }
    }
}
